/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "hhdRandom.h"
#include "hhdIO.h"
#include <math.h>

#if HHD_PLATFORM_WINDOWS
#include <windows.h>
#endif //HHD_PLATFORM_WINDOWS

namespace hhd
{
	Random::Random()
	{
		_haveNextNextGaussian = false;
		_nextNextGaussian = 0.0;
#if HHD_PLATFORM_WINDOWS
		QueryPerformanceCounter((LARGE_INTEGER*)&_val);

#endif //HHD_PLATFORM_WINDOWS
	}

	Random::Random(unsigned long long in_seed)
	{
		_val = (unsigned long long)in_seed;
		_haveNextNextGaussian = false;
		_nextNextGaussian = 0.0;
	}


	unsigned int Random::next()
	{
		_val = (_a * _val + _c) % _m;
		return (unsigned int)_val;
	}

	int Random::nextInt(int in_min, int in_max)
	{
		//int result = (int)((long long)(next() + 1) * (long long)(in_max - in_min)  / _m + in_min);
		int result = next() %  (in_max + 1 - in_min) + in_min;
		return result;
	}

	float Random::nextFloat()
	{
		return (float)next() / (float)(_m-1);
	}

	float Random::nextFloat(float in_min, float in_max)
	{
		return nextFloat() * (in_max - in_min) + in_min;
	}

	double Random::nextDouble()
	{
		return (double)next() / (double)(_m-1);
	}

	double Random::nextDouble(double in_min, double in_max)
	{
		return nextDouble() * (in_max - in_min) + in_min;
	}

	double Random::nextGaussian()
	{
		if(_haveNextNextGaussian == true) 
		{
			_haveNextNextGaussian = false;
			return _nextNextGaussian;
		} 
		else 
		{
			double v1, v2, s;
			do 
			{
				v1 = 2 * nextDouble() - 1;   // between -1.0 and 1.0
				v2 = 2 * nextDouble() - 1;   // between -1.0 and 1.0
				s = v1 * v1 + v2 * v2;
			} 
			while (s >= 1.0 || s == 0.0);
			double multiplier = sqrt(-2 * log(s)/s);
			_nextNextGaussian = v2 * multiplier;
			_haveNextNextGaussian = true;
			return v1 * multiplier;
		}
	}

	double Random::nextGaussian(double in_mean, double in_standard_deviation)
	{
		return nextGaussian() * in_standard_deviation + in_mean;
	}
}
